﻿using System;
using System.Collections.Generic;
using System.Linq;
using Shell.PocoDot.Configurations;

namespace Shell.PocoDot
{
    public class EfReversedRepository : EfRepository
    {
        public EfReversedRepository(RelationConfiguration config)
            : base(config)
        {
        }

        protected override IQueryable<Guid> GetRelatedKey(IQueryable<Relation> relations)
        {
            return relations.Select(r => r.MainId);
        }

        protected override Guid GetRelatedKey(Relation relation)
        {
            return relation.MainId;
        }

        protected override void SetMainKey(Relation relation, Guid key)
        {
            relation.RelatedId = key;
        }

        protected override void SetRelatedKey(Relation relation, Guid key)
        {
            relation.MainId = key;
        }

        protected override IQueryable<Relation> WhereKeys(Guid mainKey, Guid relatedKey)
        {
            return this.Data.Where(d => d.MainId == relatedKey && d.RelatedId == mainKey);
        }

        protected override IQueryable<Relation> WhereMainKey(Guid mainKey)
        {
            return this.Data.Where(d => d.RelatedId == mainKey);
        }
    }
}
